﻿using DevExpress.XtraEditors.Controls;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_ExamCalculateTypeItemForm_Option : BaseForm
    {
        TB_Rule_CalculateScoreDetail model = null;
        ServiceBase service = new ServiceBase();
        string CalculateScoreID = string.Empty;
        string CalculateScoreDetailID = string.Empty;
        public Base_ExamCalculateTypeItemForm_Option(string calculateScoreID, string calculateScoreDetailID)
        {
            InitializeComponent();
            CalculateScoreID = calculateScoreID;
            CalculateScoreDetailID = calculateScoreDetailID;
            this.BindSingUpType();
            this.cmbWeightType.Properties.NullText = "";
            this.cmbWeightType.Properties.Columns.Add(new LookUpColumnInfo("Key", 50, "权值类型代码"));
            this.cmbWeightType.Properties.Columns.Add(new LookUpColumnInfo("Value", 55, "权值类型名称"));
            this.cmbWeightType.Properties.DataSource = ConstAppConfig.DicExamCalculateTypeFormWeight.ToList();
            this.cmbWeightType.Properties.DisplayMember = "Value";
            this.cmbWeightType.Properties.ValueMember = "Key";
            this.cmbWeightType.Properties.NullText = "";
            BindSpecialityKinds();
            if (CalculateScoreDetailID.Length > 0)
            {
                model = service.GetExamCalculateTypeFormModel_Detail(CalculateScoreDetailID);
                if (model != null)
                {
                    model.Attach();
                    cmbSingUpType.EditValue = model.SignUpType;
                    cmbSSZY.EditValue = model.SpecialityKindsNO;
                    cmbExaminerType.EditValue = model.SpecialityKinds_DetailNO;
                    cmbWeightType.EditValue = model.Weight;
                    txtReamrk.Text = model.Remark;
                }
            }
        }

        private void BindSingUpType()
        {
            this.cmbSingUpType.Properties.NullText = "";
            this.cmbSingUpType.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKinds_DetailNO", 50, "考生类型代码"));
            this.cmbSingUpType.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKinds_DetailName", 55, "考生类型名称"));
            DataTable dt = service.GetSignUpStudentsType();
            if (dt == null)
            {
                dt = new DataTable();
                dt.Columns.Add("SpecialityKinds_DetailNO");
                dt.Columns.Add("SpecialityKinds_DetailName");
            }
            DataRow dr = dt.NewRow();
            dr["SpecialityKinds_DetailNO"] = Guid.Empty.ToSafeString();
            dr["SpecialityKinds_DetailName"] = "全部";
            dt.Rows.InsertAt(dr, 0);
            this.cmbSingUpType.Properties.DataSource = dt;
            this.cmbSingUpType.Properties.DisplayMember = "SpecialityKinds_DetailName";
            this.cmbSingUpType.Properties.ValueMember = "SpecialityKinds_DetailNO";
            this.cmbSingUpType.Properties.Columns[0].Visible = false;
            this.cmbSingUpType.EditValue = Guid.Empty.ToSafeString();
        }

        private void BindSpecialityKinds()
        {
            this.cmbSSZY.Properties.NullText = "";
            this.cmbSSZY.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKindsNO", "专业代码"));
            this.cmbSSZY.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKindsName", "专业名称"));
            DataTable dt =service.GetSpecialityKindsTable("","","","");
            if (dt == null)
            {
                dt = new DataTable();
                dt.Columns.Add("SpecialityKindsNO");
                dt.Columns.Add("SpecialityKindsName");
            }
            DataRow dr = dt.NewRow();
            dr["SpecialityKindsNO"] = Guid.Empty.ToSafeString();
            dr["SpecialityKindsName"] = "全部";
            dt.Rows.InsertAt(dr, 0);
            this.cmbSSZY.Properties.DataSource = dt;
            this.cmbSSZY.Properties.DisplayMember = "SpecialityKindsName";
            this.cmbSSZY.Properties.ValueMember = "SpecialityKindsNO";
            this.cmbSSZY.Properties.Columns[0].Visible = false;
            this.cmbSSZY.EditValue = Guid.Empty.ToSafeString();
        }

        private void BindSpecialityKindsDetail()
        {
            this.cmbExaminerType.Properties.Columns.Clear();
            this.cmbExaminerType.Properties.DataSource = null;
            this.cmbExaminerType.Properties.NullText = "";
            if (cmbSSZY.EditValue == null || cmbSSZY.EditValue.ToSafeString().Length == 0)
            {
                return;
            }
            this.cmbExaminerType.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKinds_DetailNO", "考官代码"));
            this.cmbExaminerType.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKinds_DetailName", "考官类型"));
            DataTable dt =service.GetSpecialityKindsDetailTable(cmbSSZY.EditValue.ToSafeString());
            if (dt == null)
            {
                dt = new DataTable();
                dt.Columns.Add("SpecialityKinds_DetailNO");
                dt.Columns.Add("SpecialityKinds_DetailName");
            }
            DataRow dr = dt.NewRow();
            dr["SpecialityKinds_DetailNO"] = Guid.Empty.ToSafeString();
            dr["SpecialityKinds_DetailName"] = "全部";
            dt.Rows.InsertAt(dr, 0);
            this.cmbExaminerType.Properties.DataSource = dt;
            this.cmbExaminerType.Properties.DisplayMember = "SpecialityKinds_DetailName";
            this.cmbExaminerType.Properties.ValueMember = "SpecialityKinds_DetailNO";
            this.cmbExaminerType.Properties.Columns[0].Visible = false;
            this.cmbExaminerType.EditValue = Guid.Empty.ToSafeString();
        }

        private void cmbSSZY_EditValueChanged(object sender, EventArgs e)
        {
            BindSpecialityKindsDetail();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(cmbWeightType.Text))
            {
                DevHelper.ShowErrorMessageBox("请选择权重类型！");
                cmbWeightType.Focus();
                return;
            }
            if (model == null)
            {
                model = new TB_Rule_CalculateScoreDetail();
            }
            model.CalculateScoreID = CalculateScoreID;
            model.SpecialityKinds_DetailNO = cmbExaminerType.EditValue.ToSafeString();
            model.SpecialityKindsNO = cmbSSZY.EditValue.ToSafeString();
            model.Remark = txtReamrk.Text;
            model.SignUpType = cmbSingUpType.EditValue.ToString();
            model.SignUpTypeName = cmbSingUpType.Text;
            if (cmbWeightType.EditValue != null)
            {
                model.Weight = cmbWeightType.EditValue.ToSafeDecimal();
            }
            else
            {
                model.Weight = null;
            }
            model.ExamYear = SessionerHelper.SystemYear;
            if (service.SaveExamCalculateTypeForm_Detail(model))
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
